package com.wenxy.httpclient.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.wenxy.httpclient.network.interfaces.INetWork;
import com.wenxy.httpclient.network.interfaces.NetWorkType;

public class NetWorkImpl implements INetWork {

	private ConnectivityManager mConnectivityManager; 
	public NetWorkImpl(Context context){
		mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	}
	
	@Override
	public boolean hasNetWorkActivite() {
		NetworkInfo aNetWorkInfo = mConnectivityManager.getActiveNetworkInfo();
		if(aNetWorkInfo!=null)
		return aNetWorkInfo.isAvailable();
		return false;
	}

	@Override
	public boolean wifiNetActivite() {
		NetworkInfo aNetWorkInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if(aNetWorkInfo != null)
		return aNetWorkInfo.isAvailable();
		return false;
	}

	@Override
	public boolean mobileNetActivite() {
		NetworkInfo aNetWorkInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if(aNetWorkInfo != null)
		return aNetWorkInfo.isAvailable();
		return false;
	}

	@Override
	public NetWorkType getNetWorkType() {
		NetworkInfo aNetWorkInfo = mConnectivityManager.getActiveNetworkInfo();
		if(aNetWorkInfo != null && aNetWorkInfo.isAvailable()){
			switch(aNetWorkInfo.getType()){
			case ConnectivityManager.TYPE_WIFI:
				return INetWork.NET_WROK_WIFI;
			case ConnectivityManager.TYPE_MOBILE:
			{
				if(aNetWorkInfo.getExtraInfo().toLowerCase().equals("cmnet")){
					return INetWork.NET_WROK_CMNET;
				}else{
					return INetWork.NET_WROK_CMWAP;
				}
			}
			}
		}
		return null;
	}
}
